<?php
namespace app\admin\controller;
use app\common\controller\Admin;
use app\common\model\Article as ArticleModel;
use app\common\model\Comment as CommentModel;
class Article extends Admin{
    
    /**
     * 文章管理首页
     */
    public function index(){
        $param= $this->request->param();
        $where['a.is_delete']=0;
        empty($param['start_time']) or $where['a.create_time']=['>=', strtotime($param['start_time']. "00:00:00")];
        empty($param['end_time']) or $where['a.create_time']=['<=', strtotime($param['end_time']." 23:59:59")];
        if(!empty($param['start_time'])&&!empty($param['start_time'])){
            $where['a.create_time']=['between',[strtotime($param['start_time']. "00:00:00"),strtotime($param['end_time']." 23:59:59")]];
        }
        empty($param['category_id']) or $where['a.category_id']=$param['category_id'];
        empty($param['title']) or $where['a.title']=['like','%'.$param['title'].'%'];
        if(isset($param['status'])&&$param['status']!==''){
            $where['a.status']=$param['status'];
        }
        $order='id desc';
        $this->assign('list',ArticleModel::getArticleList($where,$order));
        $this->assign('catelist', ArticleModel::getCategoryList());
        $this->assign('param',$param);
        $this->setMeta('文章管理');
        return $this->fetch('index');       
    }
    /**
     * 文章编辑
     */
    public function article_edit($id){
        $article=model('Article');
        if ($this->request->isPost()) {
            $data=$this->request->param();
            if(empty($data['keywords'])){
                $this->error('关键字不能为空');
            }
            $data['pic_list']= ArticleModel::getImgList($data['content']);
            $result = $article->save($data, array('id' => $id));
            if ($result !== false) {
                //记录行为
                action_log('update_article', 'article', $result, session('auth_user.uid'));
                return $this->success("更新成功！", url('admin/article/index'));
            } else {
                return $this->error($article->getError());
            }
        } else {
            if (!$id) {
                return $this->error("非法操作！");
            }
            $info = \think\Db::name('article a')
                    ->join('user u','u.id = a.user_id')
                    ->field('a.*,u.username')
                    ->where(['a.id'=>$id])->find();
            if (!$info) {
                return $this->error($article->getError());
            }
            $data = array(
                'info' => $info,
                'keyList' =>$article->keyList,
            );
            $template = 'public/edit';
            $this->assign($data);
            $this->setMeta("编辑文章");
            return $this->fetch($template);
        }
    }
    /**
     * 文章新增
     */
    public function article_delete(){
        $param = $this->request->param();
        $id = $param['id'];
        if (empty($id)) {
            return $this->error("非法操作！");
        }
        $map['id'] = array('IN', $id);
        $result = model('article')->where($map)->update(['is_delete'=>1]);

        if (false !== $result) {
            //记录行为
            action_log('delete_comment', 'comment', $result, session('auth_user.uid'));
            return $this->success("删除成功！");
        } else {
            return $this->error("删除失败！");
        } 
    }
    
    /**
     * 评论管理
     */
    public function comment_index(){
        $where['c.article_id']= input('id');
        $order='c.id desc';
        $this->assign('list', CommentModel::getCommentList($where,$order));
        $this->setMeta('评论管理');
        return $this->fetch('comment/index');           
    }
    /**
     * 评论编辑
     */
    public function comment_edit($id){
        $comment= model('comment');
        if ($this->request->isPost()) {
            $result =$comment->save($this->request->param(), ['id' => $id]);
            if ($result !== false) {
                return $this->success("编辑成功", url('comment_index',['id'=>$id]));
            } else {
                return $this->error($comment->getError());
            }
        } else {
            $info = db('comment')->where(array('id' => $id))->find();
            if (!$info) {
                return $this->error("非法操作！");
            }
            $data = array(
                'info' => $info,
                'keyList' => $comment->keyList,
            );
            $this->assign($data);
            $this->setMeta("编辑分类");
            return $this->fetch('public/edit');
        }
    }
    /**
     * 评论删除
     */
    public function comment_delete(){
        $param = $this->request->param();
        $id = $param['id'];
        if (empty($id)) {
            return $this->error("非法操作！");
        }

        $map['id'] = array('IN', $id);
        $result = model('comment')->where($map)->delete();

        if (false !== $result) {
            //记录行为
            action_log('delete_comment', 'comment', $result, session('auth_user.uid'));
            return $this->success("删除成功！");
        } else {
            return $this->error("删除失败！");
        }
    } 
}